package com.m4f.city.utils.tag.datatable;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class DataTableTag extends BodyTagSupport {
	//
	private String styleId;
	private String ajaxUriSource;
	
	// 
	private boolean paginate;
	private boolean lengthChange;
	private String paginationType;
	private boolean filter;
	private String ajaxDataProp;
	private String drawCallback;
	
	private boolean bProcessing = true;
	private boolean bServerSide = true;
	
	private List<IColumnTag> cols;
	
	public DataTableTag() {
		
	}
	
	public void addCol(IColumnTag col) {
		this.cols.add(col);
	}
	
	@Override
	public int doStartTag() throws JspException {
		this.cols = new ArrayList<IColumnTag>();
		this.paginate = true;
		this.lengthChange = false;
		this.paginationType = "full_numbers";
		this.filter = true;
		this.ajaxDataProp = "rows";
		this.drawCallback = "";
		
		return super.doStartTag();
	}
	
	
	@Override
	public int doEndTag() throws JspException {
		// Write here the whole table
		StringBuffer sb = new StringBuffer();
			
		sb.append(this.startScriptCode()).append("\n");
		sb.append(this.genScriptCode()).append("\n");
		
		for(IColumnTag col : cols) {
			if(col.getJsFunctionDefinition() != null || !("").equals(col.getJsFunctionDefinition())) {
				sb.append(col.getJsFunctionDefinition());
			}
		}
		
		sb.append(this.endScriptCode()).append("\n");
		sb.append(this.startHtmlTable(this.getStyleId()));
		sb.append(this.startHtmlTableHeader());
		sb.append(this.genHeader());
		sb.append(this.endHtmlHeader());
		sb.append(this.endHtmlTable());
		
		try {
			getBodyContent().getEnclosingWriter().print(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return super.doEndTag();
	}
	
	private String startHtmlTable(String id) {
		StringBuffer sb = new StringBuffer("<table id=\"").append(id).append("\"");
		
		sb.append(">");
		// Remove the last Space
		
		return sb.toString();
	}
	
	private String endHtmlTable() {
		return "</table>";
	}
	
	private String startHtmlTableHeader() {
		return "<thead>\n<tr>";
	}
	
	private String genHeader() {
		StringBuffer sb = new StringBuffer();
		
		for(IColumnTag col : cols) {
			sb.append("<th>").append(col.getI18nName()).append("</th>");
		}
		
		return sb.toString();
	}
	
	private String endHtmlHeader() {
		return "</tr>\n</thead>";
	}
	
	private String startHtmlTableFooter() {
		return "<thead>\n<tr>";
	}
	
	private String endHtmlFooter() {
		return "</tr>\n</thead>";
	}
	
	private String startScriptCode() {
		return "<script type=\"text/javascript\">";
	}
	
	private String endScriptCode() {
		return "</script>";
	}
	
	private String genScriptCode() {
		StringBuffer sb = new StringBuffer();
		sb.append("$(document).ready(function() {");
		
		sb.append("$('#").append(this.getStyleId()).append("').dataTable( {");
		
		sb.append("\"bPaginate\": true,");
		sb.append("\"bLengthChange\": false,");
		sb.append("\"sPaginationType\": \"full_numbers\",");
		sb.append("\"bProcessing\": true,");
		sb.append("\"bServerSide\": true,");
		sb.append("\"bFilter\": true,");
		sb.append("\"sAjaxSource\": \"").append(getURLBase()).append(getAjaxUriSource()).append("\","); //\"http://192.168.1.14:8080/es/admin/company/list/table\",");
		sb.append("\"sAjaxDataProp\": \"rows\",");
		
		sb.append("\"aoColumns\": [");
		for(IColumnTag col : cols) {
			sb.append(col.getColDefinition());
		}
				
		
		sb.append("],");	  			
			  		
		sb.append("});"); // end datatable
		sb.append("});"); // end ready
		return sb.toString();
	}
	
	private String getURLBase() {
		StringBuffer sb = new StringBuffer();
		sb.append("http://").append(pageContext.getRequest().getServerName());
		if(pageContext.getRequest().getLocalPort() != 80) sb.append(":").append(pageContext.getRequest().getLocalPort());
		//if(!sb.toString().endsWith("/")) sb.append("/");
		return sb.toString();
	}
	
	// GETTERS AND SETTERS
	public String getAjaxUriSource() {
		return ajaxUriSource;
	}

	public void setAjaxUriSource(String ajaxUriSource) {
		this.ajaxUriSource = ajaxUriSource;
	}

	public String getStyleId() {
		return styleId;
	}

	public void setStyleId(String styleId) {
		this.styleId = styleId;
	}
}
